﻿using System;
using SaleManagement.Web.App_Code;
using SaleManagement.Domains;
using SaleManagement.Services;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.HtmlControls;
using System.Data;

namespace SaleManagement.Web.Pages
{
  public partial class InventoryReport : BasePage
  {
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        LoadCombobox();
      }
      if (gvProduct.IsCallback)
      {
        SearchInventory();
      }
    }

    protected void cbxSearchProduct_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
      if (cbxSearchProductType.Value != null)
      {
        int productType = Convert.ToInt32(cbxSearchProductType.Value);
        TList<Product> listProduct = new ProductService().GetByGroupId(productType);
        cbxSearchProduct.DataSource = listProduct;
        cbxSearchProduct.DataBind();
        cbxSearchProduct.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem(" ", null));
      }
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
      SearchInventory();
      this.gvInventoryExport.FileName = string.Format("Nhap_xuat_ton_{0}", DateTime.Now.ToString("ddMMyyyy"));
      this.gvInventoryExport.WriteXlsToResponse();
    }

    protected void btnPdfExport_Click(object sender, EventArgs e)
    {
      this.gvInventoryExport.FileName = string.Format("Nhap_xuat_ton_{0}", DateTime.Now.ToString("ddMMyyyy"));
      this.gvInventoryExport.WritePdfToResponse();
    }
    #endregion

    #region Methods
    private void SearchInventory()
    {
      int? groupId = null;
      int? productId = null;
      //DateTime? fromDate = null;
      //DateTime? toDate = null;
      if (cbxSearchProductType.Value != null)
      {
        groupId = Convert.ToInt32(cbxSearchProductType.Value);
      }
      if (cbxSearchProduct.Value != null)
      {
        productId = Convert.ToInt32(cbxSearchProduct.Value);
      }

      gvProduct.DataSource = new ProductService().GetInventoryReport(groupId, productId, null, null);
      gvProduct.DataBind();
			gvExport.DataSource = new ProductService().GetInventoryReport(groupId, productId, null, null); ;
			gvExport.DataBind();
      ApplyProductMasterLayout();
    }

    private void ApplyProductMasterLayout()
    {
      gvProduct.BeginUpdate();
      try
      {
        gvProduct.ClearSort();
        gvProduct.GroupBy((DevExpress.Web.ASPxGridView.GridViewColumn)gvProduct.Columns["ProductId"]);
      }
      catch (Exception ex)
      {
        WriteLog("InventoryReport.aspx::Apply Master Layout", ex);
      }
      finally
      {
        gvProduct.EndUpdate();
      }
      gvProduct.ExpandAll();
    }

    private void LoadCombobox()
    {
      TList<ProductGroup> listProductCategory = new ProductGroupService().GetAll();
      cbxSearchProductType.DataSource = listProductCategory;
      cbxSearchProductType.DataBind();
      if (listProductCategory.Count > 0)
      {
        TList<Product> listProduct = new ProductService().GetByGroupId(listProductCategory[0].Id);
        cbxSearchProduct.DataSource = listProduct;
        cbxSearchProduct.DataBind();
        cbxSearchProduct.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem(" ", null));
      }
    }
    #endregion
  }
}
